package edu.zut.cs.software.sms.login.web.spring.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author WuZhiXin 
 * Servlet implementation class LoginController
 */
@WebServlet("/LoginController")
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		String Name = request.getParameter("username");
		String Passwd = request.getParameter("password");
		response.setContentType("text/html; charset=UTF-8");
		if (Name.equals("admin") && Passwd.equals("123456")) {
			response.getWriter().print("欢迎进入超市管理系统");
		} else {
			response.getWriter().print("<script language='javaScript'> alert('账号或密码错误');</script>");
//		    request.getRequestDispatcher("/WEB-INF/pages/main/index.jsp").forward(request, response);
//			 response.setHeader("refresh", "0;url=/WEB-INF/pages/main/index.jsp");
//			response.getWriter().print("Sorry UserName or Password Error!");  
	        RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/pages/main/index.jsp");  
	        rd.include(request, response);  
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
